#Boa:Frame:Frame7

import wx
import os
import commands
import time
import psycopg2
import psycopg2.extras
import sys
import pprint
from threading import Timer
from FPrincipal import FPrincipal
from FPrincipal2 import FPrincipal2
from Principal import Principal
from FAsistencia import FAsistencia



def create(parent):
    return Frame7(parent)

[wxID_FRAME7, wxID_FRAME7BUTTON1, 
] = [wx.NewId() for _init_ctrls in range(2)]

class Frame7(wx.Frame):
    def _init_ctrls(self, prnt):
        # generated method, don't edit
        wx.Frame.__init__(self, id=wxID_FRAME7, name='', parent=prnt,
              pos=wx.Point(506, 167), size=wx.Size(300, 220),
              style=wx.DEFAULT_FRAME_STYLE,
              title=u'Arq. Computadores y Ensambladores 1')
        self.SetClientSize(wx.Size(300, 220))
        self.SetBackgroundColour("White")

        self.button1 = wx.Button(id=wxID_FRAME7BUTTON1, label=u'INGRESAR',
              name='button1', parent=self, pos=wx.Point(0, 0), size=wx.Size(300,
              220), style=0)
        self.button1.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD, False,
              u'Sans'))
        self.button1.Bind(wx.EVT_BUTTON, self.OnButton1Button,
              id=wxID_FRAME7BUTTON1)
    
    def __init__(self, parent):
        self._init_ctrls(parent)

    def OnButton1Button(self, event):
        #framep=Principal(None,'001')
        #framea=FAsistencia(None,'001')
        #framep.Show(True)
        #framea.Show(True)
        conn_string = "host='localhost' dbname='DB778' user='postgres' password='123456789'"  
        dlg = wx.MessageDialog(self, 'Coloque su Huella por 10 Seg', 'Info',wx.OK | wx.ICON_INFORMATION)
        ##dlg.ShowModal()
        wx.FutureCall(5000,dlg.Destroy)
        dlg.ShowModal()
        ##dlg1 = wx.MessageDialog(self, 'Ingrese Huella por 10 Seg', 'Info' )
        ##wx.FutureCall(10000,dlg1.Destroy)
        ##dlg1.ShowModal()
        ##dlg.Destroy()
        mins = 0
        ## Only run if the user types in "start"
        ##if run == "start":
        ## Loop until we reach 20 minutes running
        ##print 'nada'
        time.sleep(2)
        os.system("./run2.sh")
        
            
##        while mins != 2:
##            #print ">>>>>>>>>>>>>>>>>>>>>", mins
##            # Sleep for a minute
##            time.sleep(1000)
##            # Increment the minute total
##            mins += 1
        dlg = wx.MessageDialog(self, 'Huella Digital Capturada', 'Info', wx.OK | wx.ICON_INFORMATION)
        ##Codigo_catedratico = codigo
        ##print Codigo_catedratico
        wx.FutureCall(5000,dlg.Destroy)
        dlg.ShowModal()
        ##dlg.Destroy()
        ##self.Close(True)
        ##event.Skip()

        conn = psycopg2.connect(conn_string)
        cursor = conn.cursor() 
        consultar_tabla="SELECT * FROM catedratico A"
        cursor.execute(consultar_tabla)
        resultados=cursor.fetchall()
        self.validar(resultados)
        cursor.close()
        conn.close()
        ##framep1=FPrincipal(None)
        ##framep1.Show()
        event.Skip()

    def OnButton2Button(self, event):
        #framep1=FPrincipal2(None)
        #framep1.Show()
        ##wx.StaticBitmap(self,-1,None,(0,0))
        event.Skip()
        
    def Inserta_Foto(self, imagenName, pos):
        imagen = open(imagenName, 'rb')
        Img = wx.BitmapFromImage(wx.ImageFromStream(imagen))
        wx.StaticBitmap(self, -1, Img, pos)
        ##self.Inserta_Foto("imagen/sistemas.jpg",(0,0))
        
    def validar(self,resultados):
        encontrado=False
        min1=0
        min2=0
	min3=0
	res=0
        codigo=''
        nombre=''
        tipo=0
        #min1 = commands.getoutput("./bozorth3 -m1 -p huella/CONVER/S1.xyt huella/CONVER2/S1.xyt")
        #print min1 
        for i in resultados:
		codigo=str(i[0])
		nombre=str(i[1])
		tipo=i[2]
		print codigo
		print tipo
		min1 = commands.getoutput("./bozorth3 -m1 -p huella/%s/S1.xyt huella/CONVER2/S1.xyt"%(codigo))
		min2 = commands.getoutput("./bozorth3 -m1 -p huella/%s/S2.xyt huella/CONVER2/S1.xyt"%(codigo))
		min3 = commands.getoutput("./bozorth3 -m1 -p huella/%s/S3.xyt huella/CONVER2/S1.xyt"%(codigo))
		res=(int(min1) + int(min2) + int(min3))/3
		if (res>=90):
			encontrado=True
			break
	
	if (encontrado==True):
		if (tipo==1):
			dlg = wx.MessageDialog(self, 'Bienvenido Administrador\n\t'+codigo+' ', 'Info', wx.OK | wx.ICON_INFORMATION)
			##Codigo_catedratico = codigo
			##print Codigo_catedratico
			wx.FutureCall(5000,dlg.Destroy)
			dlg.ShowModal()
			time.sleep(1)
			framep=Principal(None,codigo)
			framep.Show(True)
		elif (tipo==2):
			dlg1 = wx.MessageDialog(self, 'Bienvenido Usuario\n\t'+codigo+' ', 'Info', wx.OK | wx.ICON_INFORMATION)
			##Codigo_catedratico = codigo
			##print Codigo_catedratico
			wx.FutureCall(5000,dlg1.Destroy)
			dlg1.ShowModal()
			time.sleep(1)
			framea=FAsistencia(None,codigo)
			framea.Show(True)
	elif (encontrado==False):
		dlg2 = wx.MessageDialog(self, 'Huella No Registrada', 'Info', wx.OK | wx.ICON_INFORMATION)
		##Codigo_catedratico = codigo
		##print Codigo_catedratico
		wx.FutureCall(5000,dlg2.Destroy)
		dlg2.ShowModal()
